package com.openfarmanager.android.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ViewFlipper;
import com.openfarmanager.android.R;
import com.openfarmanager.android.utils.SystemUtils;

import static com.openfarmanager.android.controllers.FileSystemController.*;

/**
 * Main screen toolbar
 */
public class MainToolbar extends Fragment {

    private Handler mHandler;

//    private TextWatcher textWatcher;
    private ViewFlipper mFlipper;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(SystemUtils.isBigScreen() ? R.layout.main_toolbar : R.layout.main_toolbar_single_page, container, false);
    }

    @Override
    public void onStart() {
        super.onStart();

        View view = getView();
        mFlipper = (ViewFlipper) view.findViewById(R.id.flipper);

        View.OnTouchListener listener = new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    if (mFlipper != null) {
                        mFlipper.showNext();
                    }
                    if (!SystemUtils.isHoneycombOrNever()) {
                        view.setSelected(!view.isSelected());
                        view.setBackgroundResource(view.isSelected() ? R.color.grey_button : R.color.selected_item);
                    }
                    sendMessage(ALT_DOWN);
                } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    if (mFlipper != null) {
                        mFlipper.showPrevious();
                    }
                    if (SystemUtils.isHoneycombOrNever()) {
                        sendMessage(ALT_UP);
                    }
                }
                return true;
            }
        };

        view.findViewById(R.id.left_alt).setOnTouchListener(listener);
        view.findViewById(R.id.right_alt).setOnTouchListener(listener);

        addClickHandler(R.id.create_new, NEW);
        addClickHandler(R.id.select, SELECT);
        addClickHandler(R.id.directory_details_view, QUICKVIEW);
        addClickHandler(R.id.find, SEARCH);
        addClickHandler(R.id.menu, MENU);
        addClickHandler(R.id.diff_directories, DIFF);
        addClickHandler(R.id.help, HELP);
        addClickHandler(R.id.settings, SETTINGS);

//        textWatcher = new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//            }
//
//            @Override
//            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//                sendMessage(FILTER, charSequence.toString());
//            }
//
//            @Override
//            public void afterTextChanged(Editable editable) {
//            }
//        };
//        ((EditText) view.findViewById(R.id.main_toolbar_edittext)).addTextChangedListener(textWatcher);
    }

    public void addClickHandler(final int id, final int msg) {
        getView().findViewById(id).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendMessage(msg);
            }
        });
    }

    public void setHandler(Handler handler) {
        this.mHandler = handler;
    }

    private void sendMessage(int what) {
        if (mHandler != null) {
            mHandler.sendEmptyMessage(what);
        }
    }

    private void sendMessage(final int what, final Object obj) {
        if (mHandler != null) {
            mHandler.sendMessage(mHandler.obtainMessage(what, obj));
        }
    }

    public void resetFilter() {
//        EditText text = (EditText) getView().findViewById(R.id.main_toolbar_edittext);
//        text.removeTextChangedListener(textWatcher);
//        text.setText(null);
//        text.clearFocus();
//        InputMethodManager mgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
//        mgr.hideSoftInputFromWindow(text.getWindowToken(), 0);
//        text.addTextChangedListener(textWatcher);
    }
}